package hc.j2me.item;

import hc.j2me.Starter;
import hc.j2me.util.Jcip;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ListMenu extends Canvas implements CommandListener {
//    String iconFiles[] = {"/images/1.png","/images/2.png","/images/3.png","/images/4.png","/images/5.png","/images/6.png","/images/7.png","/images/8.png","/images/9.png",};
    String iconLabels[] = null;//{"1","2","3","4","5","6","7","8","9"};
    Image icons[] = null;//new Image[iconFiles.length];
    String[] urls = null;
    short width,height;
    short deltax,deltay;
    short focusItem = 0;
    short size;
    short[] imgx = null;//new short[9];
    short[] imgy = null;//new short[9];
    boolean fullMode, isReturnable;
    
    Command EXIT_OR_RETURN;
    
    //private Command helpCommand = new Command("help", Command.HELP, 1);
    
    public ListMenu(){
    	this(Jcip.getInstance().getString(),
    			Jcip.getInstance().getArrImage(), Jcip.getInstance().getArrString(), Jcip.getInstance().getArrString(),
    			Jcip.getInstance().getBool(), (short)Jcip.getInstance().getInt(), Jcip.getInstance().getBool()
    			);    	
    }
    
    public ListMenu(String title,
    		Image icons[], String iconLabels[], String[] urls, 
    		boolean fullMode, short size, boolean isReturnable) {        
        //display = Display.getDisplay(this);
        setFullScreenMode(fullMode);
        this.icons = icons;
        this.iconLabels = iconLabels;
        this.urls = urls;
        this.size = size;
        
        this.isReturnable = isReturnable;
        
        if(isReturnable){
			EXIT_OR_RETURN = Starter.BACK_COMMAND;
        }else{
			EXIT_OR_RETURN = Starter.EXIT_COMMAND;
        }
        
        for (int i = 0; i < size; i++) {
//			imgx[]
		}
        
        this.setTitle(title);
        
        addCommand(EXIT_OR_RETURN) ;
        setCommandListener(this) ;
    }
    
//    public void setlength(String a ,String b,short[] x,short[] y) {        
//        width = Integer.parseInt(a);//获取屏幕宽度
//        height = Integer.parseInt(b);//获取屏幕高度
//        
//        deltax = width/ncol;
//        deltay = (height-20)/nrow;
//        
//        //取图片坐标
//        imgx = x;
//        imgy = y;
//    }

    public void paint(Graphics g) {
        g.setColor(0x0FFF0F);
        g.fillRect(0,0, width, height);
        g.setColor(0x000000);
        //g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
        //g.drawString("菜单：", 0, 0, g.TOP|g.LEFT);
        g.setColor(0x000000);
        g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL));
        
//        for(short r = 0;r<nrow;r++){
//            for(short c = 0;c<ncol;c++){
//            try{
//                Image img = null;
//                //如果是焦点图片
//                if(r*ncol+c == focusItem){
//                    img = Image.createImage("/images/focus.png");                    
//                }else{
//                    img = icons[r*ncol+c];
//                }
//                g.drawImage(img, imgx[r*3+c], imgy[r*3+c], Graphics.LEFT|Graphics.TOP);
//                //System.out.println("img" + r*3+c + "x" + imgx[r*3+c]);
//                //System.out.println("img" + r*3+c + "y" + imgy[r*3+c]);
//                //g.drawImage(img, c*deltax+12, r*deltay+27, Graphics.LEFT|Graphics.TOP);
//                
//                //g.drawRect(c+deltax, r*deltay+18, img.getWidth()+4, img.getHeight()+4);
//                //g.drawString(iconLabels[r*ncol+c], c*deltax+img.getWidth()+3, r+deltay+img.getHeight(), g.LEFT|g.BASELINE);
//            }catch(Exception e){
//                e.printStackTrace();
//            }
//        }
//        }
    }

    public void commandAction(Command c, Displayable s) {
        //this.setTicker(new Ticker("sdf",));
        System.out.println("Command Action");
    }

    protected void sizeChanged(int w, int h){
    	
    }
    
    protected void hideNotify(){
//    System.out.println("屏幕被系统遮蔽") ;//会在WTK 控制台中显示，
    //读者需要注意
    }
    
    protected void showNotify(){
//    System.out.println("屏幕显示在屏幕上") ;
    }

    protected void keyPressed(int keycode) {
        //上
        if(keycode == -1){
            if(focusItem > 2){
//                focusItem = focusItem - ((short)3);
            }
        }
        //下
        else if (keycode == -2){            
            if(focusItem <= 5){
//                focusItem = focusItem + 3;
            }
            
        }
        //左
        else if (keycode == -3){            
            if(focusItem != 0 && focusItem != 3 && focusItem != 6){
//                focusItem = focusItem - 1;
            }
            
        }
        //右
        else if (keycode == -4){            
            if(focusItem != 2 && focusItem != 5 && focusItem != 8){
//                focusItem = focusItem + 1;
            }
            
        }
        this.repaint();
    }
    
    protected void keyReleased(int keycode){
//    System.out.println("Key Released");
//    pressed = false ;
//    repaint() ;
    }

    protected void keyReapeated(int keycode){
//      System.out.println("Key Released");
//      pressed = false ;
//      repaint() ;
      }
    
    protected void pointerDragged(int x, int y){
    	
    }

	protected void pointerPressed(int x, int y){
	    	
	}
	
	protected void pointerReleased(int x, int y){
		
	}
}